package cn.zxcoder.config;


import cn.zxcoder.entity.Res;
import cn.zxcoder.entity.annotation.MyLog;
import cn.zxcoder.entity.goods.Log;
import cn.zxcoder.service.ILogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@Aspect
@Slf4j
public class WebLog {


    @Value("${now.month}")
    private int mounth;

    @Autowired
    ILogService iLogServiceIml;

    @Pointcut("@annotation(cn.zxcoder.entity.annotation.MyLog)")
    public void pc1() {
    }



    @AfterReturning(returning = "res",value = "pc1()")
    public void after(JoinPoint joinPoint, Res res) {
        if (res.isSuccess()) {
            Log log = new Log();
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            //获取操作名称
            String operation = signature.getMethod().getAnnotation(MyLog.class).operation();

            // 获取RequestAttributes
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            // 从获取RequestAttributes中获取HttpServletRequest的信息
            assert requestAttributes != null;
            HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
            assert request != null;

            //获取操作人
            String username = request.getHeader("username");

            Object[] args = joinPoint.getArgs();
            List<String> names = (List<String>)args[3];
            List<String> values = (List<String>)args[2];
            System.out.println(names);
            System.out.println(values);
            for (int i = 0; i < names.size(); i++) {
                //获取操作的数量
                String value = values.get(i);
                //获取操作的商品名称
                String name = names.get(i);

                //设置属性
                log.setName(name);
                log.setOpUser(username);
                log.setOperation(operation);
                log.setOpTime(new Date());
                log.setNum(Integer.parseInt(value));

                //保存至数据库
                MonthTableNameHandler.setData(""+mounth);//分表查询
                iLogServiceIml.save(log);
            }
        }



    }

    //所有接口的日志打印
    @Pointcut("execution(* cn.zxcoder.controller..*.*(..))")
    public void pc2() {
    }

    @Before("pc2()")
    public void before(JoinPoint joinPoint) {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String remoteAddr = request.getRemoteAddr();
        String method = request.getMethod();
        String requestURL = request.getRequestURL().toString();

        log.info("请求ip：" + remoteAddr);
        log.info("请求方法：" + method);
        log.info("请求地址：" + requestURL);
        log.info("请求参数：" + Arrays.toString(joinPoint.getArgs()));

    }

    @Around("pc2()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object proceed = point.proceed();
        log.info("消耗时间：{}ms", System.currentTimeMillis() - startTime);
        return proceed;
    }
}
